import java.io.*;
import java.net.*;

public class Messenger extends Thread
{
	Socket client = null;
	BufferedReader in = null;
	PrintWriter out = null;
	private String inMessage, tableOfInformations[], inSignal;
	Table table;
	Connector connector;
	int id, iTemp;
	
	Messenger(Socket client, Table table, Connector connector, int id)
	{
		this.client=client;
		this.table=table;
		this.id=id;
		this.connector=connector;
	}
	public void run()
	{
		while(inSignal!="Q" && connector.getState(id)!=-1)
		{
			iTemp=0;
			try
			{
				out = new PrintWriter(client.getOutputStream(), true);
				in = new BufferedReader(new InputStreamReader(client.getInputStream()));
				inMessage=in.readLine();
				tableOfInformations=inMessage.split(":");
				inSignal=tableOfInformations[0];
				if(tableOfInformations[0].equals("S"))
				{
					
					if(tableOfInformations[1].equals("0")) iTemp=0;
					else if(tableOfInformations[1].equals("7")) iTemp=7;
					connector.setType(id, iTemp);
					connector.setName(id, tableOfInformations[2]);
				}
				else if(tableOfInformations[0].equals("M"))
				{
					if(tableOfInformations[1].equals("AI"))
					{
						connector.updateBet(id, connector.getTokens(id)+connector.getBet(id));
						connector.updateTokens(id, 0);
						connector.setMessage("AM:"+id+":"+connector.getBet(id)+":0:All In");
						connector.changeState(id, 2);
					}
					else if(tableOfInformations[1].equals("FD"))
					{
						connector.setMessage("AM:"+id+":"+connector.getBet(id)+":"+connector.getTokens(id)+":Fold");
						connector.changeState(id, 3);
					}
					else if(tableOfInformations[1].equals("BT"))
					{
						try
						{
							iTemp=Integer.parseInt(tableOfInformations[2]);
							if(iTemp>connector.getTokens(id)+connector.getBet(id))
							{
								connector.changeState(id, -1);
								connector.setMessage("AM:"+id+":0:0:Ban (Oszustwo)");
								table.playersInTheGame--;
							}
							else
							{
								connector.updateTokens(id, connector.getTokens(id)-(iTemp-connector.getBet(id)));
								connector.updateBet(id, iTemp);
								connector.setMessage("AM:"+id+":"+connector.getBet(id)+":"+connector.getTokens(id)+":Bet");
								connector.changeState(id, 1);
							}
						}
						catch(NumberFormatException ex)
						{
							System.out.println("Wystapil problem z zamiana ciagu znakow na liczbe!");
						}
					}
					else if(tableOfInformations[1].equals("CK"))
					{
						connector.setMessage("AM:"+id+":"+connector.getBet(id)+":"+connector.getTokens(id)+":Check");
						connector.changeState(id, 1);
					}
					else if(tableOfInformations[1].equals("CL"))
					{
						try
						{
							iTemp=Integer.parseInt(tableOfInformations[2]);
							if(iTemp>connector.getTokens(id)+connector.getBet(id))
							{
								connector.changeState(id, -1);
								connector.setMessage("AM:"+id+":0:0:Ban (Oszustwo)");
								table.playersInTheGame--;
							}
							else
							{
								connector.updateTokens(id, connector.getTokens(id)-(iTemp-connector.getBet(id)));
								connector.updateBet(id, iTemp);
								connector.setMessage("AM:"+id+":"+connector.getBet(id)+":"+connector.getTokens(id)+":Call");
								connector.changeState(id, 1);
							}
						}
						catch(NumberFormatException ex)
						{
							System.out.println("Wystapil problem z zamiana ciagu znakow na liczbe!");
						}
					}
					else if(tableOfInformations[1].equals("RS"))
					{
						try
						{
							iTemp=Integer.parseInt(tableOfInformations[2]);
							if(iTemp>connector.getTokens(id)+connector.getBet(id))
							{
								connector.changeState(id, -1);
								connector.setMessage("AM:"+id+":0:0:Ban (Oszustwo)");
								table.playersInTheGame--;
							}
							else
							{
								connector.changeState(id, 1);
								connector.updateTokens(id, connector.getTokens(id)-(iTemp-connector.getBet(id)));
								connector.updateBet(id, iTemp);
								connector.setMessage("AM:"+id+":"+connector.getBet(id)+":"+connector.getTokens(id)+":Raise");
							}
						}
						catch(NumberFormatException ex)
						{
							System.out.println("Wystapil problem z zamiana ciagu znakow na liczbe!");
						}
					}
				}
				else if(tableOfInformations[0].equals("C"))
				{
					try
					{
						if(connector.getState(id)==0)
						{
							connector.changeState(id, 1);
						}
						else if(connector.getState(id)==2)
						{
							connector.changeState(id, 4);
						}
						int[] change = new int[4];
						for(int i=0; i<4; i++)
						{
							change[i]=Integer.parseInt(tableOfInformations[i+1]);
						}
						connector.changeCards(id, change);
						connector.setMessage("AC:"+connector.getChange(id));
						out.println("INC:"+connector.getCards(id));
					}
					catch(NumberFormatException ex)
					{
						System.out.println("Wystapil problem z zamiana ciagu znakow na liczbe!");
					}
					catch(BadugiException ex)
					{
						System.out.println(ex);
					}
				}
				else if(tableOfInformations[0].equals("Q"))
				{
					if(connector.getState(id)!=-1)
					{
						connector.changeState(id, -1);
						table.playersInTheGame--;
						table.allReady=0;
					}
					break;
				}
				else
				{
					System.out.println("Nieznany komunikat od klienta!");
				}
			}
			catch(IOException ex)
			{
				System.out.println("Blad odczytu");
				System.exit(0);
			}
			catch(NullPointerException ex)
			{
				System.out.println("Blad odczytu");
				System.exit(0);
			}
		}
		if(table.playersInTheGame==0)
		{
			connector.killSerwer();
		}
	}
}